<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 接口模块
 */
namespace app\platform\controller;

use  Mscore\Core\Controller;
use  Mscore\Core\Log;
use  Mscore\Core\Lib\Upload;
use  app\platform\model\Common as Common;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class FileController extends Controller{
   
    private $header;
    /**
     * 初始化自动连接方法
     * @param array __construct 
     * @param array language 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }

    /**
     * 接受业务系统上传的图片文件
     * @param array $getLang 
     * @param array $where 
    */
    public function listdata(){
      $dir = '/static/download/'.config('config.acc');  // 文件夹路径
      if (file_exists(config('file.path').$dir)) {
        $files = scandir(config('file.path').$dir);  // 获取文件夹下所有文件和文件夹
      } else{
        $files = [];  
      }
      $data=[];
      foreach ($files as $key => $file) {
        if ($file != "." && $file != "..") {  // 排除当前目录和上层目录
           $data[$key]=['id'=>($key+1),'file'=>$file,'dir'=>$dir.'/'.$file];
        }
      }
      $data = [
            "code" => 200,
            "data" => $data,
            "count"=> count($data),
            "msg" => 'ok'
        ];
      $this->echojson($data);
    }
    /**
     * 文件列表
     * @param array $list 
     * @param array $where 
    */
    public function list(){
      $this->display('list.html');
    }
    /**
     * 删除
     * @param array $catedel 
     * @param array $where 
     */
    public function del(){
        $data = @unlink (config('file.path').post('dir'));   
        switch ($data) {
            case true:
                $this->echoJson($this->success);
                break;
            default:
                $this->echoJson($this->error);
                break;
        }
    }
}